package shizhan;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLResult;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Shizhan6 {
    public static void main(String[] args) throws DocumentException, IOException {
        SAXReader saxReader = new SAXReader();

        Document document = saxReader.read(new File("src/Phone.xml"));

        Element rootElement= document.getRootElement();

        Element brand = DocumentHelper.createElement("Brand");

        brand.addAttribute("name","OPPO");

        Element type = DocumentHelper.createElement("Type");

        type.addAttribute("name","R17");

        brand.add(type);

        rootElement.add(brand);

        OutputFormat format = OutputFormat.createPrettyPrint();

        format.setEncoding("UTF-8");

        XMLWriter xmlWriter = new XMLWriter(new FileWriter("src/Phone.xml"));

        xmlWriter.write(document);

        xmlWriter.close();
    }
}
